﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=7.3
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
	Private txtEmail As EditText
	Dim strEmail As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("frmReset")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnSubmit_Click
	strEmail = txtEmail.Text.Trim
	If strEmail = "" Then
		Msgbox("请输入电子邮件", "错误")
		Return
	End If	
	If Validate_Email(strEmail) = False Then
		Msgbox("邮箱格式不正确", "错误")
		Return
	End If
    Dim Job5 As HttpJob
	Job5.Initialize("ResetPassword", Me)
	Job5.Download2(Main.strURL & "reset-password.php", _
      Array As String("Action", "RequestPasswordReset", _
	  "Mail", strEmail))
	ProgressDialogShow("正在连接到服务器...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
	Dim res As String, action As String
    	res = Job.GetString        
		Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case "ResetPassword"
                action = parser.NextValue
                If action = "ValidEmail" Then
					Msgbox("电子邮件已发送到 " & strEmail & " 重置密码.", "重置密码")
				Else If action = "InvalidEmail" Then
					Msgbox("子邮件没有在我们的数据库中注册.", "重置密码")
                End If
		End Select
	Else
		'Log("错误: " & Job.ErrorMessage)
		ToastMessageShow("错误: " & Job.ErrorMessage, True)
	End If
	Job.Release	
End Sub

' // Source: http://www.b4x.com/android/forum/threads/validate-a-correctly-formatted-email-address.39803/
Sub Validate_Email(EmailAddress As String) As Boolean
    Dim MatchEmail As Matcher = Regex.Matcher("^(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])$", EmailAddress)
 
    If MatchEmail.Find = True Then
        'Log(MatchEmail.Match)
        Return True
    Else
		'Log("噢，请再检查一下你的电子邮件地址...")
        Return False
    End If
End Sub